package com.zjhcsoft.auth.controller;

import com.dc.eai.data.CompositeData;
import com.dc.eai.data.Field;
import com.dcfs.esb.utils.TLSimRepTools;
import com.zjhcsoft.auth.common.enums.ApiCode;
import com.zjhcsoft.auth.common.enums.PasswordType;
import com.zjhcsoft.auth.common.utils.ResultUtil;
import com.zjhcsoft.auth.common.vo.Result;
import com.zjhcsoft.auth.entity.Account;
import com.zjhcsoft.auth.entity.web.LoginBySmsRequest;
import com.zjhcsoft.auth.entity.web.LoginRequest;
import com.zjhcsoft.auth.entity.web.RegisterRequest;
import com.zjhcsoft.auth.service.LoginService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.IOException;

/**
 * 登录
 * @Author: wzya
 * @Date: 2021/3/22
 */
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    LoginService service;
    @Resource
    RedisTemplate<String, Account> redisTemplate;

    /**
     * 静态密码登录
     */
    @PostMapping("/password/static")
    @ResponseBody
    @ApiOperation(value="静态密码登录")
    public String login(HttpServletRequest request, HttpServletResponse response){
        String resultMsg="处理成功";

        try{
            BufferedInputStream is = new BufferedInputStream(request.getInputStream());
            CompositeData reqCD = TLSimRepTools.xmlToCD(IOUtils.toByteArray(is), "UTF-8");

            CompositeData sysHead = reqCD.getStruct("SYS_HEAD");
            CompositeData appHead = reqCD.getStruct("APP_HEAD");//ESB报文应用头
            CompositeData body = reqCD.getStruct("BODY");//ESB报文体
            Field fieldLoginNo = body.getField("LOGIN_NO");
            Field fieldPassword = body.getField("PASSWORD");
            Field fieldConsumerId = sysHead.getField("ORG_SYS_ID");
            Field fieldCorpCode = sysHead.getField("CORP_CODE");
            LoginRequest loginRequest = new LoginRequest();
            //报文解析
            loginRequest = (LoginRequest)ResultUtil.xmlToApiRequest(reqCD);
            Result result = service.login(loginRequest);
            result.setType(ApiCode.STATIC_LOGIN.getName());
            //返回报文组装
            resultMsg = ResultUtil.getBodyForAuth(null,sysHead,appHead,result);
        } catch (
        IOException e) {
            e.printStackTrace();
        }
        return resultMsg;
    }

    /**
     * 其他密码登录
     */
    @PostMapping("/password/others")
    @ResponseBody
    @ApiOperation(value="其他密码登录(手势、指纹、人脸")
    public Result loginByOthers(LoginRequest request){
        return service.login(request);
    }

    /**
     * 手机验证码登录
     */
    @PostMapping("/sms")
    @ResponseBody
    @ApiOperation(value="手机验证码登录")
    public Result loginBySms(LoginBySmsRequest request){
        return service.login(request);
    }

    /**
     * 登录令牌保持
     */
    @PostMapping("/keep")
    @ResponseBody
    @ApiOperation(value="登录令牌保持")
    public String loginKeep(HttpServletRequest request, HttpServletResponse response){
        String resultMsg = "";
        try{
            BufferedInputStream is = new BufferedInputStream(request.getInputStream());
            CompositeData reqCD = TLSimRepTools.xmlToCD(IOUtils.toByteArray(is), "UTF-8");
            CompositeData sys = reqCD.getStruct("SYS_HEAD");
            CompositeData app = reqCD.getStruct("APP_HEAD");
            //报文解析
            String token = (String) ResultUtil.xmlToApiRequest(reqCD);
            Result result = service.keep(token);
            result.setType(ApiCode.LOGIN_KEEP.getName());
            //返回报文组装
            resultMsg = ResultUtil.getBodyForAuth(null,sys,app,result);
        }catch (IOException e) {
            e.printStackTrace();
        }
        return resultMsg;
    }


}
